
function requiredFieldValidator(value) {
	if (value == null || value == undefined || !value.length) {
		return {valid:false, msg:"This is a required field"};
	} else {
		return {valid:true, msg:null};
	}
}

function selectMultipleRows() {
    if (!Slick.GlobalEditorLock.commitCurrentEdit()) { 
    	return; 
    }
    grid.setSelectedRows([0,1,2]);
}

$(function() {
	var grid;
	var columns = [{
	    id: "#",
	    name: "",
	    width: 40,
	    behavior: "selectAndMove",
	    unselectable: true,
	    resizable: false,
	    cssClass: "cell-reorder"
	}, {
	    id: "name",
	    name: "名称",
	    field: "name",
	    width: 100,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Text
	}, {
	    id: "code",
	    name: "货币",
	    field: "code",
	    width: 80,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Text
	}, {
	    id: "data",
	    name: "汇率",
	    field: "data",
	    width: 80,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Text
	}, {
	    id: "content",
	    name: "备注",
	    field: "content",
	    width: 150,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Text
	},{
	    id: "ok",
	    name: "",
	    field: "ok",
	    width: 50,
	    cssClass: "cell-effort-driven",	    
	    cannotTriggerInsert: true,
	    formatter: Slick.Formatters.YesNo,
	    editor: Slick.Editors.Checkbox
	}];

	var options = {
		editable: true,
	    enableAddRow: true,
	    enableCellNavigation: true,
	    asyncEditorLoading: false,
	    rowHeight: 30    
	};
	
	var grid = new Slick.Grid($("#exchange_update"), data, columns, options);
	
	/*
	grid.onAddNewRow = function addItem(newItem,columnDef) {
        var item = {name:"人民币", code:"CNY", data: "1.0", ok: true};
       
        $.extend(item, newItem);
        data.push(item);
        grid.removeRows([data.length - 1]);
        grid.updateRowCount();
        grid.render();
    };*/
    
    grid.onAddNewRow.subscribe(function (e, args) {
    	var item = {name:"人民币", code:"CNY", data: "1.0", ok: true};
		
	    $.extend(item, args.item);
	    data.push(item);
	    grid.invalidateRows([data.length - 1]);
	    grid.updateRowCount();
	    grid.render();
	});
    
    $("#button_exchange").click(function() {
    	// alert("OK");
    	// alert($.toJSON(data));
    	
    	var body = { };
    	body.data = data;
    	
    	var url = 'finance.php?do=Finance.exchangePost';
		$.post(url, body, function(data) {
			// $(result).html(data);
			alert("设置成功");
		});		
    });
    
	$('textarea').css("width", 400);
});
